/*
 * Copyright (C) 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.atwy.account.controller;

import com.atwy.account.service.AccountService;
import com.atwy.common.seatatcc.AccountServiceTCCApi;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaojing
 */
@RestController
public class AccountController implements AccountServiceTCCApi {
    private static final Logger log = LoggerFactory.getLogger(AccountController.class);
    @Autowired
    AccountService accountService;


    @Override
    public boolean prepare(BusinessActionContext businessActionContext, String userId, double money) {
        log.info("账户预处理,userId:{},money:{}", userId, money);
        return accountService.prepare(userId, money);
    }

    @Override
    public boolean commit(BusinessActionContext businessActionContext) {
        log.info("账户进行提交");
        return accountService.release(businessActionContext);
    }

    @Override
    public boolean rollback(BusinessActionContext businessActionContext) {
        return accountService.rollbackMoney(businessActionContext);
    }
}
